"""
@author:xfb
@file:13.剪刀石头布.py
@time:2024/8/23 14:33
可以创建游戏对象 游戏对象可以加入到房间
同一个房间内有两个游戏对象可以进行游戏
每次两个游戏对象随机的出剪刀石头布
三局两胜
玩家：属性 姓名 房间      方法：战斗
房间:属性 房间号  房间里成员   方法：加入玩家
"""
import random
class Player:
    def __init__(self,name):
        self.name = name
        self.room = None
    def fight(self,other):
        pass
class Room:
    def __init__(self,room_id):
        self.room_id = room_id
        self.player = []
        self.room_members = []
    def add_player(self,player):
        self.player.append(player)
b = []
c = []
Fight = Room(0)
for i in range(100):
    choices = input("1.创建房间 2.创建角色 3:将角色加入房间 4.选择房间战斗 5.退出")
    if choices == "1":
        aaa = Room(input("请输入房间号"))
        print(f"创建房间{aaa.room_id}成功")
        b.append(aaa.room_id)
    elif choices =="2":
        bbb = Player(input("请输入角色名"))
        print(f"创建角色{bbb.name}成功")
        c.append(bbb.name)
    elif choices == "3":
        room_id = input(f"正在为角色添加房间 请从以下房间中选择:{b}")
        player_name = input(f"请从以下角色中选择:{c}")
        for room in b:
            if room_id == room:
                for player in c:
                    if player_name == player:
                        Fight.add_player(player_name)
                        Fight.room_members.append(player_name)
                        print(f"将{player_name}加入房间{room_id}成功")
                        print(Fight.room_members)
    elif choices == "4":
        room_id = input(f"请从以下房间选择要进入的房间号:{b}")
        for room in b:
            if room_id == room:
                print(f"进入房间{room_id}成功")
                print("该房间有以下角色:",Fight.room_members)
                print("请选择两个角色，请勿重复选择")
                player1 = input(f"玩家1请从以下角色中选择:{Fight.room_members}")
                player2 = input(f"玩家2请从以下角色中选择:{Fight.room_members}")
                player1win = 0
                player2win = 0
                print(f"玩家{player1}和{player2}开始游戏")
                print("1,2,3分别代表 剪刀 石头 布")
                for i in range(3):
                    print(f"第{i+1}局")
                    chuzhan = ["1","2","3"]
                    p1_choice = int(random.choice(chuzhan))
                    p2_choice = int(random.choice(chuzhan))
                    print(f"{player1}的选择是{p1_choice}，{player2}的选择是{p2_choice}")
                    if p1_choice == p2_choice:
                        print(f"第{i+1}局平局")
                    elif p1_choice - p2_choice == 1 or p1_choice - p2_choice == -2:
                        print(f"第{i+1}局{player1}赢")
                        player1win += 1
                    else:
                        print(f"第{i+1}局{player2}赢")
                        player2win += 1
                    if player1win == 2:
                        print(f"最终结果{player1}胜")
                        break
                    elif player2win == 2:
                        print(f"最终结果{player2}胜")
                        break
                if (player1win == 0 and  player2win == 0) or (player1win == 1 and player2win == 1) :
                    print("最终结果是平局")
    elif choices == "5":
        print("退出程序")
        break
    else:
        print("输入错误")